const {
  $util,
} = getApp();
// 编辑记录/上传记录
Page({
  data: {
    imgs: [],
    content: '',
    startDate: '', // 开始日期
    endDate: '', // 结束日期
    startTime: '', // 开始时间
    endTime: '', // 结束时间
    isStartChange: true, 
    isEndChange: true,
  },
  onLoad(opts) {
    const isPrev = opts.openType === 'prev';
    this.setData({
      isPrev,
      ...opts,
    }, () => {
      wx.setNavigationBarTitle({
        title: isPrev ? '志愿服务记录详情' : '上传志愿服务记录',
      });
      this.loadDeatail(opts.pid, opts.id);
    });
  },
  startDateChange: function (e) {
    let { date } = this.data;
    this.setData({
      startDate: e.detail.value,
      isStartChange: e.detail.value === date 
    });
  },
  endDateChange: function (e) {
    let { startDate } = this.data;
    this.setData({
      endDate: e.detail.value,
      isEndChange: e.detail.value === startDate
    });
  },
  startTimeChange(e) {
    this.setData({
      startTime: e.detail.value,
    });
  },
  endTimeChange(e) {
    this.setData({
      endTime: e.detail.value,
    });
  },
  loadDeatail(pid, id) {
    let { isPrev } = this.data;
    $util.request({
      url: 'entry/wxapp/partyman.home.getserviceupload',
      data: {
        contentid: pid,
        recordid: id,
      },
      success: (res) => {
        let data = {};
        if (!isPrev) {
          let time = res.classifyname.starttime.split(' ');
          data.date = time[0];
          data.time = time[1];
          data.startDate = time[0];
          data.startTime = time[1];
          data.endDate = time[0];
          data.endTime = time[1];
        }
        this.setData({
          ...data,
          detail: res.data,
          isLoaded: true,
          record: res.record || {},
        });
      },
    });
  },
  setContent(e) {
    this.setData({
      content: e.detail.value,
    });
  },
  imgsOnChange(e) {
    this.setData({
      imgs: e.detail,
    });
  },
  submit() {
    const D = this.data;
    if (D.isUploading) return;
    let begintime = D.startDate + ' ' + D.startTime;
    let finishtime = D.endDate + ' ' + D.endTime;
    let dateResult = this.compareDate(D.startDate, D.endDate);
    // 0 相等 1 开始时间>结束时间 -1 开始时间<结束时间 
    let timeResult = this.compareTime(D.startTime, D.endTime);
    if (dateResult === 1 || (dateResult === 0 && timeResult !== -1)) {
      wx.showModal({
        title: '提示',
        content: '结束时间需晚于开始时间',
        showCancel: false,
      });
      return;
    }
    if (D.content.trim() === '') {
      wx.showModal({
        title: '提示',
        content: '请填写服务记录',
        showCancel: false,
      });
      return;
    }
    this.setData({
      isUploading: true,
    });
    $util.request({
      url: 'entry/wxapp/partyman.home.setserviceupload',
      data: {
        contentid: D.pid,
        record: D.content,
        img: D.imgs.toString(),
        begintime,
        finishtime,
      },
      success: (res) => {
        wx.showToast({
          title: '上传成功',
          icon: 'none',
          duration: 1000,
          mask: true,
          success: () => {
            setTimeout(() => {
              wx.navigateBack();
            }, 1000);
          },
        });
      },
    });
  },
  // 日期大小比较
  compareDate(p1, p2) {
    let v1 = p1.split('-');
    let v2 = p2.split('-');
    for (let i = 0; i < 3; i++) {
      let num1 = Number(v1[i]);
      let num2 = Number(v2[i]);
      if (num1 > num2) {
        return 1;
      } else if (num1 < num2) {
        return -1;
      }
    }
    return 0;
  },
  // 时间大小比较
  compareTime(p1, p2) {
    let v1 = p1.split(':');
    let v2 = p2.split(':');
    for (let i = 0; i < 2; i++) {
      let num1 = Number(v1[i]);
      let num2 = Number(v2[i]);
      if (num1 > num2) {
        return 1;
      } else if (num1 < num2) {
        return -1;
      }
    }
    return 0;
  },
});
